package com.maddox.relax.camera.c1

import android.hardware.Camera
import android.media.MediaRecorder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.SurfaceHolder
import androidx.databinding.DataBindingUtil
import com.maddox.relax.R
import com.maddox.relax.databinding.ActivityVideoRecoderBinding
import com.maddox.relax.util.AppUtils
import com.maddox.relax.util.LogUtil
import com.maddox.relax.util.PBitmapUtils

import java.io.File
import java.lang.Exception

/**
 * 录像功能
 */
class Camera1VideoActivity : AppCompatActivity(), SurfaceHolder.Callback {

    lateinit var RECODER_PATH: String
    lateinit var mCamera: Camera
    lateinit var surfaceHolder: SurfaceHolder
    lateinit var bd:ActivityVideoRecoderBinding

    //正在录制
    var isScanning = false
    lateinit var recoder: MediaRecorder
    var backCamera: Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bd = DataBindingUtil.setContentView(this,R.layout.activity_video_recoder)
        initView()
    }

    private fun initView() {
        bd.header.toolbarTitle.setText("录像")
        surfaceHolder = bd.surfaceview.holder
        surfaceHolder.addCallback(this)

        val videoFileDirectory = PBitmapUtils.getVideoFileDirectory(this)
        val fileName = AppUtils.getFormatDate("yyyy_MM_dd_HH_mm_ss")
        LogUtil.d("文件名:$fileName")
        val savePath = File(videoFileDirectory, "MP_${fileName}.mp4")
        if (!savePath.exists()) savePath.createNewFile()
        bd.header.toolbarBack.setOnClickListener {
            finish()
        }
        bd.exchange.setOnClickListener {
            when (backCamera) {
                true -> {
                    it.animate().rotation(180f).setDuration(300).start()

                }
                false -> {
                    it.animate().rotation(-180f).setDuration(300).start()
                }
            }
        }
        bd.btnRecoder.setOnCheckedChangeListener { buttonView, isChecked ->
            isScanning = isChecked
            when (isChecked) {
                true -> {
                    recoder = MediaRecorder()
                    recoder.setAudioSource(MediaRecorder.AudioSource.MIC)
                    recoder.setVideoSource(MediaRecorder.VideoSource.CAMERA)
                    recoder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
                    recoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
                    recoder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP)
                    recoder.setVideoSize(360, 480)
                    recoder.setVideoFrameRate(15)
                    recoder.setOutputFile(savePath.absolutePath)
                    recoder.setPreviewDisplay(surfaceHolder.surface)
                    mCamera.unlock()
                    recoder.setCamera(mCamera)
                    recoder.prepare()
                    recoder.start()

                }
                false -> {
                    recoder.stop()
                    recoder.reset()
                    recoder.release()
                }
            }
        }

    }

    private fun safeCameraOpen(id: Int): Boolean {
        return try {
            releaseCameraAndPreview()
            mCamera = Camera.open(id)
            mCamera.setPreviewDisplay(surfaceHolder)
            mCamera.startPreview()
            true
        } catch (e: Exception) {
            LogUtil.d("打开相机失败:${e.message}")
            false
        }
    }

    private fun releaseCameraAndPreview() {

    }

    override fun surfaceCreated(holder: SurfaceHolder?) {
        safeCameraOpen(Camera.CameraInfo.CAMERA_FACING_BACK)

    }

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
        mCamera.stopPreview()
        safeCameraOpen(Camera.CameraInfo.CAMERA_FACING_BACK)
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
        mCamera.setPreviewCallback(null)
        mCamera.stopPreview()
        mCamera.release()
    }


}